using System;
using System.Collections;
using System.Data;
using System.IO;
using dtSearch.Engine;
using System.Text;
using System.Windows.Forms;

namespace DTSearchHelper
{

    /// <summary>
    /// Demonstrates the use of the DataSourceToIndex member of an IndexJob, 
    /// using ADO.NET to index a database.
    /// </summary>
    public class DataTableToIndex : dtSearch.Engine.DataSource
    {
        /// <summary>
        ///  Private members 
        /// </summary>
        private DataTable fileldsDataTable = null;
        public DataTable FileldsDataTable
        {
            get { return fileldsDataTable; }
            set { fileldsDataTable = value; }
        }

        private DataTable documentsDataTable = null;
        public DataTable DocumentsDataTable
        {
            get { return documentsDataTable; }
            set { documentsDataTable = value; }
        }

        private string primaryKeyName = "";
        public string PrimaryKeyName
        {
            get { return primaryKeyName; }
            set { primaryKeyName = value; }
        }

        private int CurrentRowIndex = -1;


        /// <summary>
        /// Constructor for SampleDataSource
        /// </summary>
        public DataTableToIndex()
        {
            // Constructor initializes all private members
            DocName = "";
            DocDisplayName = "";
            DocModifiedDate = System.DateTime.Now;
            DocCreatedDate = System.DateTime.Now;
            DocText = "";
            DocFields = "";
            DocIsFile = false;
        }

        /// <summary>
        /// Initialize the data source so that the next GetNextDoc call will
        /// return the first document.
        /// </summary>
        /// <returns>
        /// Returns true if the data source was successfully initialized.
        /// </returns>
        override public bool Rewind()
        {
            bool Rewind = false;
            if (this.documentsDataTable != null && this.documentsDataTable.Rows.Count > 0)
            {
                this.CurrentRowIndex = 0;
                Rewind = true;
            }
            return Rewind;
        }

        /// <summary>
        /// Get the next document from the data source. The document
        /// information is stored in the properties.
        /// </summary>
        /// <returns>
        /// true if it succeeds, or false if there are no more rows to index.
        /// </returns>
        /// 

        override public bool GetNextDoc()
        {
            if (this.documentsDataTable.Rows.Count>0 && this.CurrentRowIndex < this.documentsDataTable.Rows.Count)
            {
                this.DocName = this.documentsDataTable.Rows[this.CurrentRowIndex][this.primaryKeyName].ToString();
                this.DocDisplayName = "";
                this.DocCreatedDate =  System.DateTime.Now;
                this.DocModifiedDate = System.DateTime.Now;
                this.DocIsFile = false;
                this.DocText = "";
                this.DocFields = this.getDocumentFields();
            }
            this.CurrentRowIndex++;
            return (this.CurrentRowIndex<=this.documentsDataTable.Rows.Count);
        }

        private string getDocumentFields()
        {
            StringBuilder sb = new StringBuilder();
            DataView dv = new DataView(this.fileldsDataTable);
            for (int i = 0; i < this.documentsDataTable.Columns.Count; i++)
            {
                string colName = this.documentsDataTable.Columns[i].ColumnName;
                string DisplayName = "";
                string SearchName = "";
                string Filter = string.Format("ColumnName='{0}'", colName);
                dv.RowFilter = Filter;
                if (dv.Count > 0)
                {
                    SearchName = dv[0].Row["SearchName"].ToString();
                    DisplayName = dv[0].Row["DisplayName"].ToString();
                }


                string colValue = this.documentsDataTable.Rows[this.CurrentRowIndex][i].ToString();
                colValue = colValue.Replace("\t", " ");

                if (SearchName.Trim() != "")
                {
                    sb.Append(SearchName + "\t" + IndexHelper.PrepareStatement(colValue, false,true) + "\t");
                }
                if (DisplayName.Trim() != "")
                {
                    sb.Append("**" + DisplayName + "\t" + colValue + "\t");
                }
            }
            return sb.ToString();
        }

    }
}